package com.hilaver.dzmis.service.impl;

import java.util.List;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.codetable.CtMaterialType;
import com.hilaver.dzmis.codetable.CtStature;
import com.hilaver.dzmis.procurement.Procurement;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductOrderItem;
import com.hilaver.dzmis.product.ProductOrderItemColor;
import com.hilaver.dzmis.product.ProductPrice;
import com.hilaver.dzmis.product.ProductYarn;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class ProductPriceServiceImpl extends AbstractBaseService {
	
	public final static String COLUMN_PI_ID = "piId";
	
	private ProductIdentificationServiceImpl piServiceImpl;
	
	private ProcurementServiceImpl pServiceImpl;
	
	private CtServiceImpl ctService;
	
	public ProductPriceServiceImpl(){
		super();
		this.piServiceImpl = new ProductIdentificationServiceImpl();
		this.ctService = new CtServiceImpl();
		this.pServiceImpl = new ProcurementServiceImpl();
	}
	@Override
	public String delete(int id) throws Exception {
		return super.delete(ProductPrice.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		ProductPrice obj = (ProductPrice)this.dao.get(ProductPrice.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		
		xmlSB.append("<" + COLUMN_PI_ID + ">");
		xmlSB.append(obj.getProductIdentification().getId());
		xmlSB.append("</" + COLUMN_PI_ID + ">");
		xmlSB.append(this.piServiceImpl.toXMLFragment(obj.getProductIdentification()));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filter) throws Exception {
		this.piServiceImpl.setLocale(this.locale);
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<ProductPrice> objList = this.dao.getAllPagination(ProductPrice.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductPrice obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			
			xmlSB.append("<" + COLUMN_PI_ID + ">");
			xmlSB.append(obj.getProductIdentification().getId());
			xmlSB.append("</" + COLUMN_PI_ID + ">");
			
			xmlSB.append("<ctStature>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtStature.class.getName(), obj.getProductIdentification().getCtStature(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctStature>");
			
			xmlSB.append(this.piServiceImpl.toXMLFragment(obj.getProductIdentification()));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductPrice.class.getName(),
				filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAll(Integer piId) throws Exception{
		return null;
	}	
	
	public ProductPrice getDefault(){
		ProductPrice pp = new ProductPrice();
		
		return pp;
	}
	
	public String doReachMin(Integer piId) throws Exception{
		StringBuffer xmlSB = new StringBuffer();
		ProductIdentification pi = this.piServiceImpl.getObj(piId);
		Procurement procurement = pi.getProcurement();
		Set<ProductIdentification> piSet = procurement.getProductIndentifications();
		
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);

		Set<ProductYarn> pySet = pi.getProductYarns();
		int offset = 0;
		for(ProductYarn py : pySet){
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<fil>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(py.getFil());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</fil>");
			
			xmlSB.append("<reference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(py.getBiYarn().getReference());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</reference>");
			
			xmlSB.append("<ctMaterialType>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtMaterialType.class.getName(), py.getBiYarn().getCtMaterialType(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctMaterialType>");
			
			xmlSB.append("<customerColor>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(py.getBiYarn().getCustomerColor());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerColor>");
			
			xmlSB.append("<factoryColor>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(py.getBiYarn().getCustomerColor());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryColor>");
			
			xmlSB.append("<pantoneNumber>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(py.getBiYarn().getPantoneColor());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</pantoneNumber>");
			
			xmlSB.append("<realColor>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(py.getBiYarn().getRealColor());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</realColor>");
			
			Float minWeight = py.getBiYarn().getMinWeight();
			xmlSB.append("<minWeight>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(minWeight);
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</minWeight>");
			
			Float orderWeight = py.getQuantity();
			xmlSB.append("<orderWeight>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(py.getQuantity());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</orderWeight>");
			
			Float totalProcurementWeight = this.pServiceImpl.getTotalWeightFromColor(piSet, py);
			xmlSB.append("<procurementWeight>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(totalProcurementWeight);
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</procurementWeight>");
			
			xmlSB.append("<doReachMin>");
			if(totalProcurementWeight >= minWeight){
				xmlSB.append(1);
			}else{
				xmlSB.append(0);
			}
			xmlSB.append("</doReachMin>");
			
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public static void main(String[] args) throws Exception{
		ProductPriceServiceImpl os = new ProductPriceServiceImpl();
		System.out.println(os.doReachMin(1));
		//char index = 'C';
		//System.out.println(++index);
	}
}



